# -*- coding: utf-8 -*-
# @Date   : 2019/11/8
# @File   : EC09_WhiteUser.py
# @Author : Cyril

'''eof
name:是否为企业征信白户
code:EC09_WhiteUser_2D
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

import sys



reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)

# 由于plutus平台out指标 只有当返回缺失值时 才触发  故当为白户时 返回缺失值

def EC09_WhiteUser():
    try:
        data = EC_DSJ_2D['data']
        EBA = getTable(data, 'EBA', 1)
        if EBA is None:
            factor = 1
        elif EBA == [{}]:
            factor = 1
        else:
            EB01 = getTable(EBA[0], 'EB01', 1)
            if EB01 is None or EB01 == [{}]:
                factor = 1
            else:
                EB01A = getTable(EB01[0], 'EB01A', 0)
                if EB01A is None or EB01A == [{}]:
                    factor = 1
                else:
                    firstLoanYear = EB01A[0].get('EB01AR01')
                    if firstLoanYear in null_type_list:
                        factor = 1
                    else:
                        factor = 0
        if factor:
            return u'缺失值'
        else:
            return 0
    except:
        return u'缺失值'


result = EC09_WhiteUser()


